<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<OBJECT classid="clsid:27984DB8-C851-439E-B625-81740482BE7C"
	id="ctl4RA1" codebase="<%=request.getContextPath()%>/printCtl4RA.cab"
	width="0" height="0"></OBJECT>
<html>
<head>
<title></title>
<link href="<%=request.getContextPath()%>/css/style.css"
	rel="stylesheet" type="text/css" media="all">
<script type="text/javascript"
	src="<%=request.getContextPath()%>/js/jquery-1.5.1.min.js"></script>
<script language="Javascript">
	$(document).ready(function() {

	});
	function printTwoCode() {
		var error;
		var account = $("#operatoraccount").val();
		var name = $("#operatorname").val();
		var refno = $("#refno").val();
		var authcode = $("#authcode").val();
		var date = new Date();
		ctl4RA1.docName = "用户证书参考号与授权码";

		if ((error = ctl4RA1.errorNo) < 0) {
			alert("error occured!");
			return false;
		}

		ctl4RA1.startPrint();

		if ((error = ctl4RA1.errorNo) < 0) {
			alert("error occured!");
			return false;
		}

		ctl4RA1.printMsg(340, 40, "用户证书参考号与授权码");
		if ((error = ctl4RA1.errorNo) < 0) {
			alert("error occured!");
			return false;
		}

		ctl4RA1.printMsg(430, 240, "账户: " + account);
		if ((error = ctl4RA1.errorNo) < 0) {
			alert("error occured!");
			return false;
		}

		ctl4RA1.printMsg(1800, 240, "姓名: " + name);
		if ((error = ctl4RA1.errorNo) < 0) {
			alert("error occured!");
			return false;
		}

		ctl4RA1.printMsg(430, 400, "参考号: " + refno);
		if ((error = ctl4RA1.errorNo) < 0) {
			alert("error occured!");
			return false;
		}
		ctl4RA1.printMsg(430, 560, "授权码: " + authcode);
		if ((error = ctl4RA1.errorNo) < 0) {
			alert("error occured!");
			return false;
		}

		ctl4RA1.printMsg(430, 720, "日期: " + date.toLocaleDateString());
		if ((error = ctl4RA1.errorNo) < 0) {
			alert("error occured!");
			return false;
		}
		ctl4RA1.endPrint();
		alert("打印结束");
		return true;
	}
</script>

<script language="VBScript">
Sub FindProviders()
		On Error Resume Next
		Dim nDefaultCSP, nCSPIndex, CspInformations, CspInformation, oOption
		Dim g_objCSPInformations
'		MsgBox "Called FindProviders"
		Set g_objCSPInformations	= window.parent.g_objClassFactory.CreateObject("X509Enrollment.CCspInformations")	    
		'Set cspInfos = CreateObject("X509Enrollment.CCspInformations")
		If ( err.number <> 0 ) then
			MsgBox Err.Number & " " & Err.Description
			window.event.returnvalue=false
			'OnSub=false
			Exit Sub
		End If
'		MsgBox "Create Object OK!"
    
		g_objCSPInformations.AddAvailableCsps
		If ( err.number <> 0 ) then
			MsgBox Err.Number & " " & Err.Description
			'OnSub=false
			window.event.returnvalue=false
			Exit Sub
		End If
'		MsgBox "Add available csp OK!"
	    
		Set CspInformations = g_objCSPInformations
			
		nDefaultCSP = -1
		' Add error message if no CSPs are found
	    
'		MsgBox "Number of CSPs: " & CspInformations.Count 
	    
		If CspInformations.Count = 0 Then
			Set oOption=document.createElement("Option")
			oOption.text="N/A"
			document.all.cryptprov.add(oOption)
		Else 
			'Loop through all CspInformation objects
			For nCSPIndex = 0 To CspInformations.Count-1
				Set CspInformation = CspInformations.ItemByIndex(nCSPIndex)
			
            If CspInformation.Name <> "Microsoft Base Cryptographic Provider v1.0" Then			
					Set oOption = document.createElement("Option")
					oOption.text = CspInformation.Name
					oOption.Value = CspInformation.Type
					document.forms(0).cryptprov.add(oOption)
				end if
									
			Next
		End If 
		
'	  MsgBox "nDefaultCSP = " & nDefaultCSP 
	  
		If nDefaultCSP <> -1 Then
			Document.all.cryptprov.selectedIndex = nDefaultCSP
		Else
			Document.all.cryptprov.selectedIndex = 0	'Select first or N/A
		End If
	End Sub	'FindProviders	

function OnSub
	' Get provider name and type
	Dim g_objEnroll, g_objPrivateKey, g_objRequest, g_objCSPInformations
		
    Dim ProviderName, ProviderType
	Dim cn
	Dim theForm
	Dim pkcs10req
	Dim X500DistinguishedName
	Dim CspInformation, CspAlgorithms, CspAlgorithm, nBestIndex, nAlgIndex
		
	Dim p10lenght
		
'	OnSub = false
	On Error Resume Next
	GetCSR = ""
	Set theForm=Document.forms(0)
	
	Set g_objEnroll	= window.parent.g_objClassFactory.CreateObject("X509Enrollment.CX509Enrollment")
	If ( err.number <> 0 ) then
		MsgBox Err.Number & " ===1" & Err.Description
		OnSub=false
		window.event.returnvalue=false
		exit function
	End If
		
	Set g_objPrivateKey	= window.parent.g_objClassFactory.CreateObject("X509Enrollment.CX509PrivateKey")
	If ( err.number <> 0 ) then
		MsgBox Err.Number & " ===2" & Err.Description
		OnSub=false
		window.event.returnvalue=false
		exit function
	End If

	Set g_objRequest = window.parent.g_objClassFactory.CreateObject("X509Enrollment.CX509CertificateRequestPkcs10")
	If ( err.number <> 0 ) then
		MsgBox Err.Number & " ===3" & Err.Description
		OnSub=false
		window.event.returnvalue=false
		exit function
	End If
	Set g_objCSPInformations	= window.parent.g_objClassFactory.CreateObject("X509Enrollment.CCspInformations")	
	If ( err.number <> 0 ) then
		MsgBox Err.Number & " " & Err.Description
		OnSub=false
		window.event.returnvalue=false
		exit function
	End If
	g_objCSPInformations.AddAvailableCsps
	If ( err.number <> 0 ) then
		MsgBox Err.Number & " " & Err.Description
		OnSub=false
		window.event.returnvalue=false
		exit function
	End If
	set options = document.forms(0).cryptProv.options
	index = options.selectedIndex
		
	ProviderName = options(index).text
	ProviderType = options(index).value
		
    g_objPrivateKey.ProviderName = ProviderName
		
    g_objPrivateKey.ProviderType = ProviderType
		
'	MsgBox ProviderName
'	MsgBox ProviderType
		
	  g_objPrivateKey.Length = 1024
		
    g_objPrivateKey.KeySpec = 2	'AT_SIGNATURE

    g_objPrivateKey.MachineContext = false
		
    g_objPrivateKey.KeyProtection = 1	' (XCN_NCRYPT_UI_PROTECT_KEY_FLAG = 1)
		
    g_objPrivateKey.ExportPolicy = 1	' (XCN_NCRYPT_ALLOW_EXPORT_FLAG = 1)
	  
	' Initialize
		
    Call g_objRequest.InitializeFromPrivateKey(1, g_objPrivateKey, "")	'X509CertificateEnrollmentContext.ContextUser

	Set X500DistinguishedName = window.parent.g_objClassFactory.CreateObject("X509Enrollment.CX500DistinguishedName")
	If ( err.number <> 0 ) then
		MsgBox Err.Number & " " & Err.Description
		OnSub=false
		window.event.returnvalue=false
		exit function
	End If

	'cn="cn=" & theForm.refNo.value
		
    Call X500DistinguishedName.Encode(cn, 0)	'XCN_CERT_NAME_STR_NONE
		
    g_objRequest.Subject = X500DistinguishedName
		
'	MsgBox cn
		
	' Set hash algo		

    Set CspInformation = g_objCSPInformations.ItemByName(ProviderName)
'	MsgBox cn
'	MsgBox Err.Number
	If ( err.number <> 0 ) then
		MsgBox Err.Number & " " & Err.Description
		OnSub=false
		window.event.returnvalue=false
		exit function
	End If

'	MsgBox "Set CspInfomation"
	Set CspAlgorithms = CspInformation.CspAlgorithms
'	MsgBox Err.Number
	If ( err.number <> 0 ) then
		MsgBox Err.Number & " " & Err.Description
		OnSub=false
		window.event.returnvalue=false
		exit function
	End If
		
'	MsgBox "Set CspAlgorithms"
'	nBestIndex = 0
'	Msgbox "CspAlgorithms.Count= " &CspAlgorithms.Count
		
'   For nAlgIndex=0 To CspAlgorithms.Count-1
'		MsgBox "CspAlgorithms.Name = " & CspAlgorithms.ItemByIndex(nAlgIndex).Name
'		If CspAlgorithms.ItemByIndex(nAlgIndex).Name = "md5" Then
'			nBestIndex = nAlgIndex
'		End If
'		If CspAlgorithms.ItemByIndex(nAlgIndex).Name = "md5" AND CspAlgorithms.ItemByIndex(nBestIndex).Name <> "sha1" Then
'			nBestIndex = nAlgIndex
'		End If
		
'       Next
'		MsgBox "nBestIndex= " & nBestIndex

'		Set CspAlgorithm = CspAlgorithms.ItemByIndex(nBestIndex)
'		MsgBox "before md5"
		Set CspAlgorithm = CspAlgorithms.ItemByName("md5")
		Set AlgorithmOid = CspAlgorithm.GetAlgorithmOid(0, 0)
'		MsgBox "CspAlgorithm.OID = " & AlgorithmOid.Value
'		MsgBox "after md5"
	If ( err.number <> 0 ) then
		MsgBox Err.Number & " " & Err.Description
		OnSub=false
		window.event.returnvalue=false
		exit function
	End If
				
'		MsgBox "set md5"
		
	If CspAlgorithm.Type = 2 Then	
            'XCN_CRYPT_HASH_INTERFACE
			
    	g_objRequest.HashAlgorithm = AlgorithmOid	', AlgorithmFlagsNone
'		MsgBox "MD5 oid " & AlgorithmOid.Value
		
	End if
		
        
    ' Try to create request
		
'	MsgBox "InitializeFromRequest"
		
	g_objEnroll.InitializeFromRequest(g_objRequest)
		
'	MsgBox "CreateRequest"

'	pkcs10req = g_objEnroll.CreateRequest(3)	CRYPT_STRING_BASE64REQUESTHEADER
	pkcs10req = g_objEnroll.CreateRequest(3)	


	If ( err.number <> 0 ) then
		MsgBox Err.Number & " " & Err.Description
		OnSub = false
		window.event.returnvalue=false
		exit function
	End If		
		
	If(pkcs10req=Empty) Then
		errorString="The error '" & hex(Err.Number) & "' occurred."& chr(13) & chr(10) & "Your certificate request could not be generated."
		err=MsgBox(errorString,0,"Internet Explorer Certificate Request")
		MsgBox(errorString)
		window.event.returnvalue=false
		OnSub=false
		on error goto 0
		exit function
	End If
		
			
'	MsgBox "p10length = " & Len(pkcs10req)		
'	p10length = Len(pkcs10req) - 36 - 34			
'	MsgBox "p10length = " & p10length		
'	theForm.myKey.Value=Mid(pkcs10req , 37 , p10length)

	pkcs10req = Replace(pkcs10req, "-----BEGIN NEW CERTIFICATE REQUEST-----", " " )
	pkcs10req = Replace(pkcs10req, "-----END NEW CERTIFICATE REQUEST-----", " " )
  
    'MsgBox pkcs10req
	theForm.myKey.Value=pkcs10req
	theForm.csp.Value=options(index).text
End function
</script>
</head>
<body leftmargin=0 topmargin=0 marginheight="0" marginwidth="0"
	background="images/bg_right.gif" onload="FindProviders()">
	<br />
	<table width="80%" border="0" cellpadding="0" cellspacing="0"
		align="center">
		<tr>
			<td>
				<table width="85%" align="center" cellpadding=5 cellspacing=0>
					<tr>
						<td align=center><span class="alert"> ${msg} </span></td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td>
				<form
					action="<%=request.getContextPath()%>/operator/downloadPerUserCert.html"
					method="post">
					<table width="80%" border=0 align="center" cellpadding="3"
						cellspacing="1" bgcolor="#cccccc">
						<tr>
							<td width="15%" class=list01>账户持有人名称 <input type="hidden"
								name="ID" value="${perUserCert.ID}" />
							</td>
							<td width="35%" class=list02>
								${perUserCert.accountholdername}</td>
							<td width="15%" class=list01>账户持有人简称</td>
							<td width="35%" class=list02>
								${perUserCert.accountholderabbre}</td>
						</tr>
						<tr>
							<td width="15%" class=list01>证书申请方式</td>
							<td width="35%" class=list02>${requestType}</td>
							<td width="15%" class=list01>证书SN</td>
							<td width="35%" class=list02>${perUserCert.certSN}</td>
						</tr>
						<tr>
							<td width="20%" class=list01>证书类型</td>
							<td width="30%" class=list02>${certType}</td>
							<td width="20%" class=list01>USBKEYID</td>
							<td width="30%" class=list02>${perUserCert.usbkeyid}</td>
						</tr>
						<tr>
							<td width="15%" class=list01>CSP</td>
							<td width="35%" class=list02 colspan="4"><select
								id="cryptProv">
									<OPTION SELECTED VALUE="1">Microsoft Base
										Cryptographic Provider v1.0
							</select> <input type="hidden" name="myKey" /> <input type="hidden"
								name="usertype" value="1" /> <font color="red"> </font></td>
						</tr>
						<tr>
							<td width="15%" class=list03 colspan="4"><input
								type="submit" value="代为下载证书" onclick="OnSub()" />
								&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="button" onclick="printTwoCode();" value="打印两码" />
							</td>
						</tr>
					</table>
				</form>
			</td>
		</tr>
	</table>
</body>
</html>